<?php


namespace App\Http\Controllers\Admin;


use App\Exports\ExcelExport;
use App\Http\Controllers\Controller;
use App\Models\Notice;
use App\Models\NoticeJs;
use App\Services\NoticeService;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class NoticeController extends Controller
{
    private NoticeService $service;
    private Request $request;
    public function __construct(Request $request,NoticeService $service)
    {
        $this->service = $service;
        $this->request = $request;
    }

    public function list(){
        $res = $this->service->list($this->request);
        return response()->json($res);
    }
    public function detail($id){
        $detail =  Notice::query()->find($id);
        return ResponseUtil::success($detail);
    }

    public function export(){
        $res = $this->service->list($this->request);
        $excelRows = [
            ['ID','标题','创建时间']
        ];
        foreach($res['data'] as $datum){
            $excelRows[] = [
                $datum['id'], $datum['title'],$datum['created_at']
            ];
        }
        $excel = new ExcelExport($excelRows);
        return Excel::download($excel, '通知公告.xlsx');
    }

    public function delete(){
        $ids = $this->request->post('ids');
        Notice::query()->whereIn('id',$ids)->delete();
        return ResponseUtil::success();
    }

    public function add(){
        $post = $this->request->post();
        $data = array_only($post,['title','content','status','type','sorts']);
        $type = data_get($data,'type');
        if ($type==='' || is_null($type)){
            return ResponseUtil::error("请选择类型");
        }
        $data['created_at'] = now();
        Notice::query()->insert($data);
        return ResponseUtil::success();
    }

    public function edit($id){
        $post = $this->request->post();
        $data = array_only($post,['title','content','status','type','sorts']);
        $data['updated_at'] = now();
        Notice::query()->where('id',$id)->update($data);
        return ResponseUtil::success();
    }


    //微变版处理方法
    public function listJs(){
        $res = $this->service->listJs($this->request);
        return response()->json($res);
    }
    public function detailJs($id){
        $detail =  NoticeJs::query()->find($id);
        return ResponseUtil::success($detail);
    }

    public function exportJs(){
        $res = $this->service->listJs($this->request);
        $excelRows = [
            ['ID','标题','创建时间']
        ];
        foreach($res['data'] as $datum){
            $excelRows[] = [
                $datum['id'], $datum['title'],$datum['created_at']
            ];
        }
        $excel = new ExcelExport($excelRows);
        return Excel::download($excel, '通知公告.xlsx');
    }

    public function deleteJs(){
        $ids = $this->request->post('ids');
        NoticeJs::query()->whereIn('id',$ids)->delete();
        return ResponseUtil::success();
    }

    public function addJs(){
        $post = $this->request->post();
        $data = array_only($post,['title','content','status','type','sorts']);
        $type = data_get($data,'type');
        if ($type==='' || is_null($type)){
            return ResponseUtil::error("请选择类型");
        }
        $data['created_at'] = now();
        NoticeJs::query()->insert($data);
        return ResponseUtil::success();
    }

    public function editJs($id){
        $post = $this->request->post();
        $data = array_only($post,['title','content','status','type','sorts']);
        $data['updated_at'] = now();
        NoticeJs::query()->where('id',$id)->update($data);
        return ResponseUtil::success();
    }


}
